package com.spgf.view.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;

import com.archtecture.model.patterns.PadraoActivityMnt;
import com.archtecture.model.patterns.PadraoFragmentMnt;
import com.spgf.controller.R;
import com.spgf.model.entities.utils.ModelAb;

public class DialogManutencao extends Dialog implements OnClickListener {

	@SuppressWarnings("rawtypes")
	private PadraoActivityMnt	activity;

	@SuppressWarnings({"rawtypes"})
	private PadraoFragmentMnt	activityFrag;

	private Button				btnCancelar, btnEditar, btnExcluir;

	private ModelAb				model;

	@SuppressWarnings("rawtypes")
	public DialogManutencao(PadraoActivityMnt pActivity, ModelAb pModel) {
		super(pActivity);
		this.activity = pActivity;
		this.model = pModel;
	}

	@SuppressWarnings("rawtypes")
	public DialogManutencao(PadraoFragmentMnt pActivity, ModelAb pModel) {
		super(pActivity.getActivity());
		this.activityFrag = pActivity;
		this.model = pModel;
	}

	@Override
	public void onClick(View pView) {

		switch (pView.getId()) {

			case R.id.btn_dialog_manutencao_editar:

				Class<?> lClasse;
				Activity lActivity;

				if (activity != null) {
					lClasse = activity.getControleCadastro();
					lActivity = activity;
				} else {
					lClasse = activityFrag.getControleCadastro();
					lActivity = activityFrag.getActivity();
				}

				Intent lIntent = new Intent(lActivity, lClasse);
				lIntent.putExtra("CODIGO", model.getCodigo());
				lActivity.startActivity(lIntent);

				break;

			case R.id.btn_dialog_manutencao_excluir:

				if (activity != null) {
					activity.configurarExcluir(model.getCodigo());
				} else {
					activityFrag.configurarExcluir(model.getCodigo());
				}

				break;
		}

		dismiss();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.dialog_manutencao);

		btnCancelar = (Button) findViewById(R.id.btn_dialog_manutencao_cancelar);
		btnCancelar.setOnClickListener(this);

		btnEditar = (Button) findViewById(R.id.btn_dialog_manutencao_editar);
		btnEditar.setOnClickListener(this);

		btnExcluir = (Button) findViewById(R.id.btn_dialog_manutencao_excluir);
		btnExcluir.setOnClickListener(this);

	}
}